package com.xiangyi.teachingsystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.xiangyi.teachingsystem.entity.po.GroupReview;
import com.xiangyi.teachingsystem.entity.vo.Result;
import com.xiangyi.teachingsystem.service.GroupReviewService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class GroupReviewController {

    @Autowired
    private GroupReviewService groupReviewService;

    @PostMapping("/addReview")
    public Result addReview(@Validated @RequestBody GroupReview groupReview){
        try {
            groupReviewService.save(groupReview);
            return Result.success("评价成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }
    }

    @PostMapping("/updateReview")
    public Result updateReview(@Validated @RequestBody GroupReview groupReview){
        try {
            groupReviewService.updateById(groupReview);
            return Result.success("修改成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }
    }

    @GetMapping("/groupRewListByTaskId")
    public Result groupRewListByTaskId(@RequestParam Integer tsakId){
        try {
            List<GroupReview> groupReviewList = groupReviewService.getBaseMapper().selectList(new QueryWrapper<GroupReview>()
                    .eq("task_id", tsakId));
            return Result.success("小组互评结果查询成功").setData(groupReviewList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }
    }

    @GetMapping("/groupRewListByIdGroup")
    public Result groupRewListByIdGroup(@RequestParam Integer idGroup){
        try {
            List<GroupReview> groupReviewList = groupReviewService.getBaseMapper().selectList(new QueryWrapper<GroupReview>()
                    .eq("id_group", idGroup));
            return Result.success("小组互评结果查询成功").setData(groupReviewList);
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }
    }

}
